#include "BinTriNodePool.h"
#include "BinTriNode.h"
#include <iostream>


BinTriNodePool::BinTriNodePool(int size)
{
	mPool.resize(size);
	index=0;
	initlize=true;
}

BinTriNodePool::~BinTriNodePool(void)
{
}

BinTriNode * BinTriNodePool::GetNewNode()
{
	BinTriNode * ret=NULL;
	if (initlize)
	{
		ret=&mPool[index++];
		//std::cout<<"index is "<<index<<std::endl;
		if (index>=mPool.size())
		{
			initlize=false;
		}
	}
	else{
		if (!mAvilable.empty())
		{
			ret=mAvilable.front();
			ret->Reset();
			mAvilable.pop_front();
		}
		else
		{
			ret=NULL;
			throw exception("no more BintriNode!"); 
			//ret=new BinTriNode;
			//create new node will lead to memory leak
		}
	}
	return ret;

}

void BinTriNodePool::Recycle( BinTriNode * node )
{
	if (node){
		mAvilable.push_front(node);
	//	delete node;
	//	node=NULL;
	}
}

//BinTriNodePool&  BinTriNodePool::GetInstance()
//{
//	static BinTriNodePool mPool;
//	return mPool;
//}